﻿using Prism.Commands;
using StarryEdge.Core.Models;
using StarryEdge.Core.Parameter;
using StarryEdge.Menu.User.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Menu.User.ViewModels
{
    public class ButtonEditViewModel
    {
        public ObservableCollection<UIRoleItem> UIRoles { get; set; }
        private IUserEntity _userEntity;
        private IUIRoleManage _roleManage;
        public ButtonEditViewModel(IUserEntity userEntity, IUIRoleManage roleManage)
        {
            _roleManage = roleManage;
            _userEntity = userEntity;
            UIRoles = new ObservableCollection<UIRoleItem>();
            var blos = roleManage.GetUIRoleModels(x => x.Level == 3);
            foreach (var blo in blos)
            {
                var twos = roleManage.GetUIRoleModels(x => x.Level == 3 && !string.IsNullOrEmpty(x.Name) && x.Title == blo.Title);
                var citem = new UIRoleItem(default, false)
                {
                    Name = blo.Name,
                    Title = blo.Title,
                    Level = blo.Level,
                };
                foreach (var item in blo.GuidRoles)
                {
                    citem.GuidRoles.Add(item);
                }
                UIRoles.Add(citem);
                foreach (var one in twos)
                {
                    var child = new UIRoleItem(citem, true)
                    {
                        Name = one.Name,
                        Title = one.Title,
                        Level = one.Level,
                    };
                    foreach (var item in one.GuidRoles)
                    {
                        child.GuidRoles.Add(item);
                    }
                    citem.AddChild(child);
                    UIRoles.Add(child);
                }
            }
        }

        public DelegateCommand AllRole => new DelegateCommand(() =>
        {
            foreach (var item in UIRoles)
            {
                if (item.IsEdit == System.Windows.Visibility.Visible)
                {
                    var role = _roleManage.GetUIRoleModel(x => x.Level == item.Level && x.Title == item.Title && item.Name == x.Name);
                    if (role != null)
                    {
                        foreach (var roleitem in _userEntity.GetRoles())
                        {
                            var model = new UIRoleItemModel()
                            {
                                Id = roleitem.Id,
                                Name = roleitem.Name
                            };
                            if (!role.GuidRoles.Any(x => x.Id == roleitem.Id))
                            {
                                item.GuidRoles.Add(model);
                                role.GuidRoles.Add(model);
                            }
                        }
                    }
                }
            }
            _roleManage.Save();
        });

        public DelegateCommand<UIRoleItem> AddRole => new DelegateCommand<UIRoleItem>(sender =>
        {
            if (sender != null)
            {
                MaterialDesignThemes.Wpf.DialogHost.Show(new Views.Dialog.EditRole() { DataContext = new Dialog.EditRolePopupDialog(_userEntity, _roleManage, sender) }, Core.Enums.PopUpTypes.Global.ToString());
            }
        });

        public DelegateCommand<UIRoleItem> DeleteRole => new DelegateCommand<UIRoleItem>(sender =>
        {
            if (sender != null && sender.SelectUIRole != null)
            {
                var model = sender.GuidRoles.FirstOrDefault(x => x.Id == sender.SelectUIRole.Id);
                if (model != null)
                {
                    var role = _roleManage.GetUIRoleModel(x => x.Level == sender.Level && x.Title == sender.Title && sender.Name == x.Name);
                    if (role != null)
                    {
                        var myguid = role.GuidRoles.FirstOrDefault(x => x.Id == model.Id);
                        if (myguid != null)
                        {
                            role.GuidRoles.Remove(myguid);
                        }
                    }
                    sender.GuidRoles.Remove(model);
                    _roleManage.Save();
                }
            }
        });
    }
}
